package com.example.config.shiro;

import com.example.util.shiro.RedisUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @application:
 * @author: LiYuhang
 * @Date: 2021/7/26 9:58
 * @version: 0.1
 * @Description :
 * @Modified By:
 */
@Configuration
public class ShiroConfig {
    @Value("${spring.shiro.excludeUrls:#{''}}")
    private String excludeUrls;

    @Value("${spring.redis.port:#{6379}}")
    private int port;

    @Value("${spring.redis.database:#{0}}")
    private int database;

    @Value("${spring.redis.host:#{'127.0.0.1'}}")
    private String host;

    @Value("${spring.redis.password:#{''}}")
    private String redisPassword;

    private static final Logger log = LoggerFactory.getLogger(ShiroConfig.class);

    private int expire = 30*60;

    @Autowired
    RedisUtil redisUtil;

    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager){
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        // 拦截器
        Map<String, String> filterChainDefinitionMap = new LinkedHashMap<String, String>();
        if(StringUtils.isNotBlank(excludeUrls)){
            String[] permissionUrl = excludeUrls.split(",");
            for(String url : permissionUrl){
                filterChainDefinitionMap.put(url,"anon");
            }
        }
        /*authc:所有url都必须认证通过才可以访问; anon:所有url都都可以匿名访问*/
        // 配置不会被拦截的链接 顺序判断
        filterChainDefinitionMap.put("/static/**", "anon");
        filterChainDefinitionMap.put("/logout", "logout");
        filterChainDefinitionMap.put("/**", "authc");
        Map<String, Filter> filtersMap = new LinkedHashMap<String, Filter>();

        filtersMap.put("filter", new ShiroLoginFilter(redisUtil));
        shiroFilterFactoryBean.setFilters(filtersMap);
        shiroFilterFactoryBean.setLoginUrl("/login");
        // 登录成功后要跳转的链接
        shiroFilterFactoryBean.setSuccessUrl("/index");
        // 未授权界面返回
        shiroFilterFactoryBean.setUnauthorizedUrl("/403");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
        return shiroFilterFactoryBean;
    }
    @Bean
    public HashedCredentialsMatcher hashedCredentialsMatcher(){
        HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
        //散列算法:这里使用MD5算法;
        hashedCredentialsMatcher.setHashAlgorithmName("md5");
        //散列的次数，比如散列两次，相当于 md5(md5(""));
        hashedCredentialsMatcher.setHashIterations(2);
        return hashedCredentialsMatcher;
    }

    @Bean
    public ShiroRealm myShiroRealm(){
        ShiroRealm shiroRealm = new ShiroRealm();
        shiroRealm.setCredentialsMatcher(hashedCredentialsMatcher());
        return shiroRealm;
    }


}
